﻿Imports System.Collections.Specialized
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Windows.Media.Animation
Imports System.Windows.Threading
Imports System.IO
Imports System.Net
Imports System.Threading
Imports Infragistics.Windows.DockManager
Imports Infragistics.Windows.Ribbon
Imports System.ComponentModel

Class MainWindow
    Inherits Infragistics.Windows.Ribbon.XamRibbonWindow
    Implements Controlers.ITeamSystemView(Of MainWindow)
    Implements Controlers.IHeatView(Of MainWindow)
    Implements Controlers.IQueryView(Of MainWindow)


    Private m_AboutWindow As AboutWindow
    Private m_CredentialProviderBox As CredentialProviderBox
    Private m_HeatConnectionDialog As HeatConnectionDialog

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        Try
            InitializeComponent()
        Catch ex As Exception
            Dim x As Integer = 1
        End Try
    End Sub

    Private Sub MainWindow_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        My.Settings.Save()
        My.Application.Shutdown()
    End Sub

    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Application.ControlerFactory.AddControler(New Controlers.TeamSystemControler(Of MainWindow)(Me))
        Application.ControlerFactory.AddControler(New Controlers.HeatControler(Of MainWindow)(Me))
        Application.ControlerFactory.AddControler(New Controlers.QueryControler(Of MainWindow)(Me))
        ' Create other windows
        m_AboutWindow = New AboutWindow
        m_CredentialProviderBox = New CredentialProviderBox
        m_HeatConnectionDialog = New HeatConnectionDialog
        '--------------------------
        ' Set TFS bits
        uxTeamSystemRibbonGroup.DataContext = TeamSystemControler
        uxHeatITSMRibbonGroup.DataContext = Me.HeatControler
        uxQuery3ContentPane.DataContext = Me.QueryControler
        floatingDockable1.DataContext = Me.QueryControler
    End Sub

    Public ReadOnly Property DockManager() As Infragistics.Windows.DockManager.XamDockManager Implements IView.DockManager
        Get
            Return uxDockManager
        End Get
    End Property

    Public ReadOnly Property Ribbon() As Infragistics.Windows.Ribbon.XamRibbon Implements IView.Ribbon
        Get
            Return uxXamRibbon
        End Get
    End Property

    ''' <summary>
    ''' Command handler for changing the selected team project
    ''' </summary>
    Private Sub CodeplexCommand_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
        Dim Param As String = e.Parameter
        Dim nav As String = "http://hinshelwood.com/TFSHeatITSM.aspx"
        Dim Title As String = "Hinshelwood.com"
        Select Case Param
            Case "Home"
                nav = "http://hinshelwood.com/TFSHeatITSM.aspx"
            Case "Documentation"
                nav = "http://www.codeplex.com/TFSHeatITSM/"
            Case "Release"
                nav = "http://www.codeplex.com/TFSHeatITSM/Release/ProjectReleases.aspx"
                Title = "Latest Release"
            Case "Discussions"
                nav = "http://www.codeplex.com/TFSHeatITSM/Thread/List.aspx"
                Title = "Discussions"
            Case "IssueTracker"
                nav = "http://www.codeplex.com/TFSHeatITSM/WorkItem/AdvancedList.aspx"
                Title = "Issue Tracker"
            Case "SourceCode"
                nav = "http://www.codeplex.com/TFSHeatITSM/SourceControl/ListDownloadableCommits.aspx"
                Title = "Source Code"
            Case "License"
                nav = "http://www.codeplex.com/TFSHeatITSM/license"
                Title = "License"
            Case "Martin"
                nav = "http://blog.hinshelwood.com"
                Title = "Martin Hinshelwood's Blog"
            Case Else
                nav = "http://hinshelwood.com"
                Title = "Hinshelwood.com"
        End Select
        CreateInternetView(Title, New Uri(nav))
        ' System.Diagnostics.Process.Start(nav)
    End Sub



    Private Sub ShowAboutCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        m_AboutWindow.Visibility = Windows.Visibility.Visible
        m_AboutWindow.Owner = Me
        m_AboutWindow.ShowDialog()
    End Sub

    Private Sub ClearSettingsCommand_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
        My.Settings.Reset()
        My.Settings.Reload()
        TeamSystemControler.Reset()
        HeatControler.Reset()
    End Sub

    ''' <summary>
    ''' Command handler for logo click event
    ''' </summary>
    Private Sub RDdotNet_MouseLeftButtonUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        ' Open the RDdotNet website in the user's default browser
        CreateInternetView("RDdotNet", New Uri("http://hinshelwood.com/TFSHeatITSM.aspx"))
    End Sub

    Public Sub CreateInternetView(ByVal Title As String, ByVal Uri As Uri)
        Dim frame As New Frame
        frame.Source = Uri
        Dim newContentPane As ContentPane = Me.uxDockManager.AddDocument(Title, frame)
        newContentPane.Name = Title
        newContentPane.Activate()
    End Sub

#Region " ITeamSystemView "

    Public ReadOnly Property TeamSystemControler() As Controlers.TeamSystemControler(Of MainWindow) Implements Controlers.ITeamSystemView(Of MainWindow).TeamSystemControler
        Get
            Return Application.ControlerFactory.GetControler(Of Controlers.TeamSystemControler(Of MainWindow))()
        End Get
    End Property

    Public Function GetCredentialProvider() As Microsoft.TeamFoundation.Client.ICredentialsProvider Implements Controlers.ITeamSystemView(Of MainWindow).GetCredentialProvider
        Return Me.m_CredentialProviderBox
    End Function

    Private Sub SelectTeamServerCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        TeamSystemControler.SelectedServer = DirectCast(e.Parameter, TeamFoundationServer)
    End Sub

    Private Sub SelectTeamProjectCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        TeamSystemControler.SelectedProject = DirectCast(e.Parameter, Project)
    End Sub

    Private Sub SelectTeamProjectCommand_CanExecuted(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
        e.CanExecute = Not TeamSystemControler.SelectedServer Is Nothing And Not TeamSystemControler.AvailableProjects.Count = 0
    End Sub


#End Region

#Region " IHeatView "

    Public ReadOnly Property HeatControler() As Controlers.HeatControler(Of MainWindow) Implements Controlers.IHeatView(Of MainWindow).HeatControler
        Get
            Return Application.ControlerFactory.GetControler(Of Controlers.HeatControler(Of MainWindow))()
        End Get
    End Property

    Private Sub SelectConnectionCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        m_HeatConnectionDialog = New HeatConnectionDialog
        m_HeatConnectionDialog.Owner = Me
        m_HeatConnectionDialog.ShowDialog()
        HeatControler.Reset()
    End Sub


    Private Sub RefreshAvailableCommand_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        ' HeatControler.Refresh()
    End Sub

#End Region
#Region " IQueryView "

    Public ReadOnly Property QueryControler() As Controlers.QueryControler(Of MainWindow) Implements Controlers.IQueryView(Of MainWindow).QueryControler
        Get
            Return Application.ControlerFactory.GetControler(Of Controlers.QueryControler(Of MainWindow))()
        End Get
    End Property

#End Region




End Class
